from pydantic import BaseModel
from dataclasses import dataclass
from typing import List,Any,Callable,Optional

@dataclass
class OrderItem:
    column:str
    asc:bool = True

    def __repr__(self):
        return f"{self.column} {'ASC' if self.asc else 'DESC'}"

class Page(BaseModel):
    #每页大小，默认10
    size:int =10
    #当前页，默认1
    current:int=1
    #总数
    total:int=0
    # 当前页的数据
    records:List[Any] = None
    # 排序字段
    orders:List[OrderItem] = None

    def __post_init__(self):
        if self.records is None:
            self.records = []
        if self.records is None:
            self.orders = []


    def offset(self)->int:
        return max((self.current - 1)*self.size,0)


    def pages(self)->int:
        if self.size == 0:
            return 0
        return (self.total//self.size)+(1 if self.total % self.size > 0 else 0)